from PIL import Image, ImageEnhance

###################################
# 功能：给指定图片添加图片水印
###################################

# 调整水印的透明度
def reduce_dissolve(img, dissolve):
    # dissolve_resized = dissolve / 100
    assert dissolve >= 1 and dissolve <= 100
    if img.mode != 'RGBA':
        img = img.convert('RGBA')
    else:
        img = img.copy()
    alpha = img.split()[3]
    alpha = ImageEnhance.Brightness(alpha).enhance((dissolve/100))
    img.putalpha(alpha)
    return img

def imgWatermark(im, mark, dissolve, gravity, dx, dy, ws):
    # dissolve:透明度|gravity:水印位置|dx:横轴边距|dy:纵轴边距|ws:水印图片自适应原图的短边比例，取值范围0-1
    # 原始图片尺寸
    origin_w, origin_h = im.size[0], im.size[1]
    # 水印图片尺寸
    mark_w, mark_h = mark.size[0], mark.size[1]
    # 水印图片长短比例
    ratio = mark_w / mark_h
    print("原始图片尺寸：", origin_w, origin_h)
    print("水印图片尺寸：", mark_w, mark_h)
    # 水印透明度判断
    if dissolve >= 1 and dissolve <= 100:
        mark = reduce_dissolve(mark, dissolve)
    # 创建一张新图片
    layer = Image.new('RGBA', im.size, (0, 0, 0, 0))

    # 化零为整
    if dx:
        dx = int(dx)
    elif not dx:
        dx = 10
    if dy:
        dy = int(dy)
    elif not dy:
        dy = 10

    a = origin_w/3
    a = int(a)
    b = origin_h/3
    b = int(b)

    # 水印图片自适应原图的短边比例，取值范围0-1
    # 求原始图片短边长度
    if origin_w > origin_h:
        shortside = origin_h
    else:
        shortside = origin_w
    if mark_w > mark_h:
        mark_h = shortside * ws
        mark_w = mark_h * ratio
    else:
        mark_w = shortside * ws
        mark_h = mark_w / ratio

    mark_w = int(mark_w)
    mark_h = int(mark_h)

    print("要求水印图片大小：", mark_w, mark_h)
    # 更新水印图片大小
    # mark.size = (mark_w, mark_h)
    # print("未更新水印图片大小：", mark.size[0], mark.size[1])
    # 对水印图片进行缩放处理
    # mark.thumbnail(mark_w, mark_h)
    size = (mark_w, mark_h)
    # 缩放后水印尺寸
    # print("缩放后水印尺寸：", size[0], size[1])
    mark = mark.resize(size, Image.ANTIALIAS)
    # mark.show()
    print("更新后的水印图片尺寸：", mark.size[0], mark.size[1])
    print("-----------------------------------------------------")
    # 水印位置判断
    if gravity == 'NorthWest':
        layer.paste(mark, (0 + dx, 0 + dy))
    elif gravity == 'North':
        layer.paste(mark, (a + dx, 0 + dy))
    elif gravity == 'NorthEast':
        layer.paste(mark, (a * 2 + dx, 0 + dy))
    elif gravity == 'West':
        layer.paste(mark, (0 + dx, b + dy))
    elif gravity == 'Center':
        layer.paste(mark, (a + dx, b + dy))
    elif gravity == 'East':
        layer.paste(mark, (a * 2 + dx, b + dy))
    elif gravity == 'SouthWest':
        layer.paste(mark, (0 + dx, b * 2 + dy))
    elif gravity == 'South':
        layer.paste(mark, (a + dx, b * 2 + dy))
    elif gravity == 'SouthEast':
        layer.paste(mark, (a * 2 + dx, b * 2 + dy))
    elif not gravity:
        layer.paste(mark, (a * 2 + dx, b * 2 + dy))
    return Image.composite(layer, im, layer)

# 测试例
im = Image.open("D:\\test.jpg")
mark = Image.open("D:\\test1.jpg")
imgWatermark(im, mark, 50, 'NorthWest', 50, 50, 0.1).show()
imgWatermark(im, mark, 50, 'NorthWest', 50, 50, 0.1).save("D:\\图片云\\1.jpg")
imgWatermark(im, mark, 50, 'North', 50, 50, 0.1).show()
imgWatermark(im, mark, 50, 'North', 50, 50, 0.1).save("D:\\图片云\\2.jpg")
imgWatermark(im, mark, 50, 'NorthEast', 50, 50, 0.1).show()
imgWatermark(im, mark, 50, 'NorthEast', 50, 50, 0.1).save("D:\\图片云\\3.jpg")
imgWatermark(im, mark, 50, 'West', 50, 50, 0.1).show()
imgWatermark(im, mark, 50, 'West', 50, 50, 0.1).save("D:\\图片云\\4.jpg")
imgWatermark(im, mark, 50, 'Center', 50, 50, 0.1).show()
imgWatermark(im, mark, 50, 'Center', 50, 50, 0.1).save("D:\\图片云\\5.jpg")
imgWatermark(im, mark, 50, 'East', 50, 50, 0.1).show()
imgWatermark(im, mark, 50, 'East', 50, 50, 0.1).save("D:\\图片云\\6.jpg")
imgWatermark(im, mark, 50, 'SouthWest', 50, 50, 0.1).show()
imgWatermark(im, mark, 50, 'SouthWest', 50, 50, 0.1).save("D:\\图片云\\7.jpg")
imgWatermark(im, mark, 50, 'South', 50, 50, 0.1).show()
imgWatermark(im, mark, 50, 'South', 50, 50, 0.1).save("D:\\图片云\\8.jpg")
imgWatermark(im, mark, 50, 'SouthEast', 50, 50, 0.1).show()
imgWatermark(im, mark, 50, 'SouthEast', 50, 50, 0.1).save("D:\\图片云\\9.jpg")
